import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home'
import List from '@/views/List'
import Cart from '@/views/Cart'
import My from '@/views/My'

// import(/* webpackChunkName: "about" */ '../views/About.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/list',
    name: 'list',
    component: List,
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/cart',
    name: 'cart',
    component: Cart,
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/my',
    name: 'my',
    component: My,
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/search',
    name: 'search',
    component: () => import(/* webpackChunkName: "search" */ '@/views/Search'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/search-list',
    name: 'search-list',
    component: () => import(/* webpackChunkName: "search-list" */ '@/views/SearchList'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import(/* webpackChunkName: "detail" */ '@/views/Detail'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '@/views/Login'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/login-code',
    name: 'login-code',
    component: () => import(/* webpackChunkName: "loginCode" */ '@/views/LoginCode'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/setting',
    name: 'setting',
    component: () => import(/* webpackChunkName: "Setting" */ '@/views/Setting'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/payment',
    name: 'payment',
    component: () => import(/* webpackChunkName: "payment" */ '@/views/Payment'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/path',
    name: 'path',
    component: () => import(/* webpackChunkName: "path" */ '@/views/Path'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/addPath',
    name: 'addPath',
    component: () => import(/* webpackChunkName: "addpath" */ '@/views/AddPath'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/pay-success',
    name: 'pay-success',
    component: () => import(/* webpackChunkName: "paySuccess" */ '@/views/paySuccess')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  const nextRoute = ['cart', 'payment', 'addPath', 'pay-success']
  const uesrInfo = JSON.parse(localStorage.getItem('userInfo'))
  if (nextRoute.indexOf(to.name) >= 0) {
    // 需要验证的路由
    if (!uesrInfo) {
      router.push('/login')
    }
  }
  if (to.name === 'login') {
    if (uesrInfo) {
      router.push('/home')
    }
  }
  next()
})
export default router
